// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of Ubiquity formsPlayer
//
// Ubiquity formsPlayer is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// Ubiquity formsPlayer is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with Ubiquity formsPlayer If not, see <http://www.gnu.org/licenses/>.
//	
//
//   Purpose: declaration of CXFormsElementDefaultImpl Templates.
//   $Id$


#pragma once
#include "xformselement.h"
/*
template <class TImpl, class TParse, class TUI > class CXFormsElementDefaultImpl:
	public CXFormsElement
		*/


template <class TImpl, class TParse, class TUI,class TBase = CXFormsElement > class CXFormsElementDefaultImpl:
	public TBase
{
public:
	CXFormsElementDefaultImpl(void){};
	virtual ~CXFormsElementDefaultImpl(void){};

	
	virtual CImpl* CreateImpl()
	{
		// Bug 877 - Cast to CFormsControl * so that we get the right inheritance 
		// branch for CImplAction inherited classes with mulptie inheritance. 
		CFormControl* pFormControl = (CFormControl*) new TImpl(static_cast<TUI*>(this));
		return (CImpl*) pFormControl;
	}

	virtual CXFormsParse* CreateParse(CImpl* pImpl)
	{
		//TODO: this dynamic cast is only needed because of 
		//	the multiple inheritance mess in CImplAction descendents.
		//	when that is fixed, change to static_cast
		CXFormsParse* pParse = new TParse(dynamic_cast<TImpl*>(pImpl));

		return pParse;
	}
};

template <class TImpl, class TParse, class TUI,class TBase = CXFormsElement > class CXFormsElementInvisibleDefaultImpl:
	public CXFormsElementDefaultImpl <TImpl, TParse, TUI,TBase>
{
public:
	CXFormsElementInvisibleDefaultImpl(void){};
	virtual ~CXFormsElementInvisibleDefaultImpl(void){};
	virtual HRESULT render(void)
	{
		HRESULT hr = E_FAIL;
		CComPtr<IHTMLElement> pEl;
		GetMasterElement(&pEl);
		CComQIPtr<IHTMLElement2> pEl2 = pEl;
		if(pEl2)
		{
			CComPtr<IHTMLStyle>pStyle;
			pEl2->get_runtimeStyle(&pStyle);
			CComBSTR bs = L"none";
			pStyle->put_display(bs);
			hr = S_OK;
		}
		return hr;
	}
protected:
	virtual void RegisterFindBehaviourRefusal(){};
	virtual void RegisterFindBehaviourAcceptance(){};

};
	
template <class TBase> class CXFormsElementDefaultListenerImpl
	:public TBase
	,public IDispatchImpl<IEventListener, &IID_IEventListener, &LIBID_DOM2EventsLib,2,0>
{
public:
	CXFormsElementDefaultListenerImpl(void){};
	~CXFormsElementDefaultListenerImpl(void){};

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	BEGIN_COM_MAP(CXFormsElementDefaultListenerImpl<TBase>)
		COM_INTERFACE_ENTRY(IEventListener)
		COM_INTERFACE_ENTRY_CHAIN(CXFormsElement)
	END_COM_MAP()

	STDMETHOD(handleEvent)(IDOMEvent * /* pEvent*/)
	{
		HRESULT hr = E_FAIL;
		CImplAction * pAction = dynamic_cast<CImplAction*>(m_pImpl);
		if(pAction)
		{
			//This is not strictly correct. 
			//	The event may not be one for which invokation
			//	is registered.  However, I don't think that
			//	that is likely.
			pAction->Invoke();
		}
		return hr;
	}

	virtual HRESULT InsertBranding(void) {/* REMOVE BRANDING */ return S_OK; };
};
